##########################################################################################
## TITLE:	  Compulsory voting rules, reluctant voters and proximity			                ##
## AUTHORS:	Dassonneville, Feitosa, Hooghe, Lau & Stiers 					                      ##
## PURPOSE:	Replication of main results, matching Swiss data clogit (CV with sanctions)	##
##########################################################################################


###############################
####### Setting Up R      #####
###############################

#### Installing Packages ####
## Note: You only have to install packages when using them for the first time on your computer

install.packages("RcppArmadillo")
install.packages("MatchIt")
install.packages("optmatch")

#### Loading Libraries ####
## Note: You have to install libraries every time you open R. 

library(foreign)
library(MatchIt)

###############################
####### Loading Data      #####
###############################


#### Loading Data ####

setwd("/Users/ruthdassonneville/Box Sync/Compulsory Voting and correct voting/Replication materials dataverse")
data1 <- read.dta("Switzerland_coded_clogit.dta")
dim(data1)
names(data1)
head(data1)



#######################################################
####### Matching - Create matched dataset   		  #####
#######################################################

### Subsetting the Datasets by year ####
myvars <- c("voteclosest", "female", "age", "education", "pid", "cv_rules", "lrs", "year", "cv_weak", "cv_strict",
            "voteparty1", "voteparty2", "voteparty3", "voteparty4", "voteparty5", "voteparty6", 
            "voteparty7", "voteparty8", 
            "distparty1", "distparty2", "distparty3", "distparty4", "distparty5", "distparty6",
            "distparty7", "distparty8",
            "partisanparty1", "partisanparty2", "partisanparty3", "partisanparty4", "partisanparty5", "partisanparty6", 
            "partisanparty7", "partisanparty8",
             "relpract")
votedata71 <- data1[myvars]

myvars <- c("voteclosest", "female", "age", "education", "pid", "cv_rules", "lrs", "year", "cv_weak", "cv_strict", "cv_strictvsno",
            "voteparty1", "voteparty2", "voteparty3", "voteparty4", "voteparty5", "voteparty6", 
             "voteparty8", "voteparty9", "voteparty10", "voteparty11",
            "distparty1", "distparty2", "distparty3", "distparty4", "distparty5", "distparty6",
            "distparty8", "distparty9", "distparty10", "distparty11",
            "partisanparty1", "partisanparty2", "partisanparty3", "partisanparty4", "partisanparty5", "partisanparty6", 
            "partisanparty8", "partisanparty9", "partisanparty10", "partisanparty11",
             "relpract")
votedata95 <- data1[myvars]

myvars <- c("voteclosest", "female", "age", "education", "pid", "cv_rules", "lrs", "year", "cv_weak", "cv_strict", "cv_strictvsno",
            "voteparty1", "voteparty2", "voteparty3", "voteparty4", "voteparty5", "voteparty6", 
             "voteparty8", "voteparty9", "voteparty10", 
            "distparty1", "distparty2", "distparty3", "distparty4", "distparty5", "distparty6",
             "distparty8", "distparty9", "distparty10",
            "partisanparty1", "partisanparty2", "partisanparty3", "partisanparty4", "partisanparty5", "partisanparty6", 
            "partisanparty8", "partisanparty9", "partisanparty10",
             "relpract")
votedata99 <- data1[myvars]

myvars <- c("voteclosest", "female", "age", "education", "pid", "cv_rules", "lrs", "year", "cv_weak", "cv_strict", "cv_strictvsno",
            "voteparty1", "voteparty2", "voteparty3", "voteparty4", "voteparty5",  
            "voteparty7",  "voteparty9", "voteparty10", 
            "distparty1", "distparty2", "distparty3", "distparty4", "distparty5", 
            "distparty7",  "distparty9", "distparty10", 
            "partisanparty1", "partisanparty2", "partisanparty3", "partisanparty4", "partisanparty5", 
            "partisanparty7",  "partisanparty9", "partisanparty10", 
             "relpract")
votedata03 <- data1[myvars]

myvars <- c("voteclosest", "female", "age", "education", "pid", "cv_rules", "lrs", "year", "cv_weak", "cv_strict", "cv_strictvsno",
            "voteparty1", "voteparty2", "voteparty3", "voteparty4", "voteparty5", 
            "voteparty7",  "voteparty9", "voteparty10", 
            "distparty1", "distparty2", "distparty3", "distparty4", "distparty5", 
            "distparty7",  "distparty9", "distparty10",
            "partisanparty1", "partisanparty2", "partisanparty3", "partisanparty4", "partisanparty5", 
            "partisanparty7",  "partisanparty9", "partisanparty10", 
             "relpract")
votedata07 <- data1[myvars]

myvars <- c("voteclosest", "female", "age", "education", "pid", "cv_rules", "lrs", "year", "cv_weak", "cv_strict", "cv_strictvsno",
            "voteparty1", "voteparty2", "voteparty3", "voteparty4", "voteparty9", "voteparty10", 
            "distparty1", "distparty2", "distparty3", "distparty4",  "distparty9", "distparty10",
            "partisanparty1", "partisanparty2", "partisanparty3", "partisanparty4", "partisanparty9", 
            "partisanparty10",  "relpract")
votedata11 <- data1[myvars]

votedata71 <-votedata71[votedata71[, "year"] == 1971,]
votedata95 <-votedata95[votedata95[, "year"] == 1995,]
votedata99 <-votedata99[votedata99[, "year"] == 1999,]
votedata03 <-votedata03[votedata03[, "year"] == 2003,]
votedata07 <-votedata07[votedata07[, "year"] == 2007,]
votedata11 <-votedata11[votedata11[, "year"] == 2011,]


votedata71 <- na.omit(votedata71)
votedata95 <- na.omit(votedata95)
votedata99 <- na.omit(votedata99)
votedata03 <- na.omit(votedata03)
votedata07 <- na.omit(votedata07)
votedata11 <- na.omit(votedata11)


### 1971

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata71, method="nearest", caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.71 <- match.data(m.out1) 
  write.dta(m.vote.71, "m.votestrict71_clogit-caliper2.dta") 

### 1995

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata95, method="nearest", caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.95 <- match.data(m.out1) 
  write.dta(m.vote.95, "m.votestrict95_clogit-caliper2.dta") 

### 1999

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata99, method="nearest", caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.99 <- match.data(m.out1) 
  write.dta(m.vote.99, "m.votestrict99_clogit-caliper2.dta") 

### 2003

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata03, method="nearest",caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.03 <- match.data(m.out1) 
  write.dta(m.vote.03, "m.votestrict03_clogit-caliper2.dta") 

### 2007

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata07, method="nearest",caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.07 <- match.data(m.out1) 
  write.dta(m.vote.07, "m.votestrict07_clogit-caliper2.dta") 

### 2011

  ### Matching - no sophistication  ###
  set.seed(12345)
  m.out1 <- matchit(cv_strict ~ female+age+education+pid+lrs+relpract,data=votedata11, method="nearest",caliper=0.2)
  summary(m.out1)

  ### Saving the Data ###
  m.vote.11 <- match.data(m.out1) 
  write.dta(m.vote.11, "m.votestrict11_clogit-caliper2.dta") 





